package com.fingard.dsp.bank.directbank.cqcb01;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;


public class CQCB01Base extends DirectBase implements ITcpClient {
	protected void writeCommonHeader(XmlTextWriter xmlWriter, CQCB01Header p_bankHeader) throws Exception{
		if(StringHelper.isNullOrEmpty(p_bankHeader.transCode))
		{
			throw new Exception("未赋值transCode");
		}
		xmlWriter.writeStartElement("in");
		xmlWriter.writeStartElement("head");
		xmlWriter.writeElementString("TransCode", p_bankHeader.transCode);
		xmlWriter.writeElementString("TransDate",new SimpleDateFormat("yyyyMMdd").format(new Date()));
		xmlWriter.writeElementString("TransTime",new SimpleDateFormat("HHmmss").format(new Date()));
		xmlWriter.writeElementString("MasterId",bankFront.loginName);
		xmlWriter.writeElementString("PacketId",new SimpleDateFormat("yyyyMMddHH").format(new Date()));
		xmlWriter.writeEndElement();
		xmlWriter.writeStartElement("body");

	}
	
	protected String[] sendToBank(String p_reqStr) throws Exception{

		String[] retStr = new String[]{"",""};

		WriteBankLogLn("1.提交银行的地址：" + bankFront.serverURL + ":" + bankFront.serverPort);

		//发送银行前置
		TcpPortClient tmpClient = new TcpPortClient(bankFront.serverURL,Integer.parseInt(bankFront.serverPort),this);

		String tmpCharset = "GBK";
		byte[] tmpBytesBody = p_reqStr.getBytes(tmpCharset);

		WriteBankLogStep2(p_reqStr);
		TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesBody);
		retStr[0] = tmpReceiver.connState;
		if(tmpReceiver.recBytes != null){
			retStr[1] = new String(tmpReceiver.recBytes,0, tmpReceiver.readedLength, tmpCharset);
			WriteBankLogStep3(retStr[1]);
		}
		else{
			retStr[1] = tmpReceiver.getConnMsg();
		}
		if(!tmpReceiver.isCompleted){
			WriteBankLogLn(tmpReceiver.getConnMsg());
		}
		return retStr;
	}
	// 发送签名地址TCP/IP协议传送
	protected String[] sendToBankSignXml(String signXml) throws Exception{
		String[] retStr = new String[]{"",""};

		WriteBankLogLn(bankFront.serverIP+":"+bankFront.signPort);

		//发送银行前置
		TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP,Integer.parseInt(bankFront.signPort),this);
		String tmpCharset = "GBK";
		byte[] tmpBytesBody = signXml.getBytes(tmpCharset);
		WriteBankLogStep2(signXml);
		TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesBody);
		retStr[0] = tmpReceiver.connState;
		if(tmpReceiver.recBytes != null){
			retStr[1] = new String(tmpReceiver.recBytes,0, tmpReceiver.readedLength, tmpCharset);
			WriteBankLogStep3(retStr[1]);
		}
		else{
			retStr[1] = tmpReceiver.getConnMsg();
		}
		if(!tmpReceiver.isCompleted){
			WriteBankLogLn(tmpReceiver.getConnMsg());
		}
		WriteBankLogLn("4.签名返回的结果：" + retStr[1]);
		return retStr;
	}
	protected static boolean isActiveRetCode(String retCode){
		if(retCode != null){
			if (retCode.equals("0000")){
				return true;
			}
		}
		return false;
	}

	@Override
	public void getContentLength(TcpReceiver p_receiver) throws Exception {
		if (p_receiver.contentLength == -1) {
			String tmpCharset = "GBK";
			String tmpRecNow = new String(p_receiver.recBytes, 0, p_receiver.readedLength, tmpCharset);
			if (tmpRecNow.contains("</out>")) {
				p_receiver.contentLength = p_receiver.readedLength;
				p_receiver.contentStartIndex = 0;
			}
		}
	}
}
